/**
 * 后台功能
 * */


(function () {
})();


//获取功能列表
function getList(){
    $.ajax({
        type: apis.actionIndex.method,
        url: apis.actionIndex.url,
        data: { type: type },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#pageTotal').html(res.total);
            var h = '', i, i2, childs;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].pname + '</td>';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].link + '</td>';
                h += '<td class="f-14">';
                h += '<a title="编辑" href="javascript:;" onclick="list_edit(\'编辑\',\'action-edit.html\', ' + d[i].id + ')" style="text-decoration:none">';
                h += '修改';
                h += '<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
                h += '<input type="hidden" id="link' + d[i].id + '" value="' + d[i].link + '"/>';
                h += '<input type="hidden" id="pid' + d[i].id + '" value="' + d[i].pid + '"/>';
                h += '</td>';
                h += '</tr>';
                childs = d[i].childs;
                if (childs.length > 0) {
                    for (i2 = 0; i2 < childs.length; i2 ++) {
                        h += '<tr class="text-c">';
                        h += '<td>' + childs[i2].pname + '</td>';
                        h += '<td>' + childs[i2].title + '</td>';
                        h += '<td>' + childs[i2].link + '</td>';
                        h += '<td class="f-14">';
                        h += '<a title="编辑" href="javascript:;" onclick="list_edit(\'功能编辑\',\'action-edit.html\',' + childs[i2].id + ')" style="text-decoration:none">';
                        h += '修改';
                        h += '<i class="Hui-iconfont">&#xe6df;</i>';
                        h += '</a>';
                        h += '</td>';
                        h += '</tr>';
                    }
                }
            }
            $('tbody').html(h)
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取一个
function getOne(v){
    $.ajax({
        type: apis.actionOne.method,
        url: apis.actionOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            $('#title')[0].value = res.data.title;
            //$('#link')[0].value = res.data.link;
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function list_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,300);
}


//保存更新
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        link: $('#link').val(),
        pid: $('#pid').val(),
    };
    if (!formData.title) {
        msgPop({msg: '功能名称必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.actionModify.method,
        url: apis.actionModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '修改成功'});
            setTimeout(function(){
                //layer_close();
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}